package com.baike.mq;

import com.baike.feign.OrderServiceFeign;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@RocketMQMessageListener(topic = "timeout-topic",consumerGroup = "timeout-group")
public class RocketMQConsumer implements RocketMQListener,RocketMQPushConsumerLifecycleListener {

    @Resource
    private OrderServiceFeign orderServiceFeign;
    @Resource
    private RedisTemplate redisTemplate;


    //延时消息消费者,解决超时未支付问题
    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //获取message
                MessageExt messageExt = list.get(0);
                //消息的重试次数
                int reconsumeTime = messageExt.getReconsumeTimes();
                try {
                    //解决消息消费幂等性问题
                    Boolean b = redisTemplate.boundHashOps("mqmessage").hasKey(messageExt.getMsgId());
                    if (!b){
                        //消息内容
                        byte[] body = messageExt.getBody();
                        String str = new String(body);
                        String[] split = str.split(",");
                        String outTradeNo = split[0];
                        String typeId = split[1];
                        if ("1".equals(typeId)){//广告订单的
                            //修改广告订单状态
                            orderServiceFeign.TimeOutAdOrderByOutTradeNo(outTradeNo);
                        }else if ("2".equals(typeId)){//课程订单的
                            //修改课程订单转台
                            orderServiceFeign.orderTimeOut(outTradeNo);
                        }else if ("3".equals(typeId)){//拼团订单的
                            //拼团订单状态修改/与课程订单共用
                            orderServiceFeign.orderTimeOut(outTradeNo);
                        }
                        //消费成功后将消息放入redis
                        redisTemplate.boundHashOps("mqmessage").put(messageExt.getMsgId(),body);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    //自定义死信
                    if (reconsumeTime>2){
                        //放入死信队列,并返回消费成功
                        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                    }
                    //返回失败
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }

                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }

    @Override
    public void onMessage(Object o) {

    }
}
